//
//  FirstViewController.m
//  艾薇贝儿
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 dllo－－邴鹏超. All rights reserved.
//

#import "FirstViewController.h"
#import "DcAFNetWorkTool.h"
#import "DcFirstModel.h"
#import "SDCollectionViewCell.h"
#import "SDCycleScrollView.h"
#import "MainViewController.h"
#import "UIImageView+WebCache.h"
#import "Dc_HotViewController.h"
#import "Dc_HotTableViewCell.h"
#import "Dc_CollectionViewCell.h"
#import "Dc_FTableViewCell.h"
#import "Dc_FCTableViewCell.h"
#import "Dc_HotViewController.h"
#import "Dc_PlayerViewController.h"
#import "Dc_PlayerSTK.h"
#import "Dc_Dan.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "DcSongsModel.h"
#import "Dc_XinGeViewController.h"
#import "Dc_LunFViewController.h"

@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource,Dc_HotTableViewCellDelegate,SDCycleScrollViewDelegate>
//{
//    UITableView * _tableView;
//    NSMutableArray * _array;
//    NSMutableArray * _douArray;
//    NSMutableArray * _xinArray;
//    SDCycleScrollView * _sdcScroll;
//    NSMutableArray * _picArray;
//    NSArray * _arr;
//}
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *douArray;
@property (nonatomic, retain)NSMutableArray *xinArray;
@property (nonatomic, retain)SDCycleScrollView *sdcScroll;
@property (nonatomic, retain)NSMutableArray *picArray;
@property(nonatomic,strong)MBProgressHUD * HUD;
@property (nonatomic, retain)NSArray *arr;

@end

@implementation FirstViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
  [self http];
    
    self.title=@"边听边看";
    _douArray = [[NSMutableArray alloc]init];
    _array=[[NSMutableArray alloc]init];
    _xinArray=[[NSMutableArray alloc]init];
    
    UIImageView * beiImage=[[UIImageView alloc]initWithFrame:self.view.frame];
    [beiImage setImage:[UIImage imageNamed:@"background.jpg"]];
    beiImage.userInteractionEnabled=YES;
    [self.view addSubview:beiImage];
    
    
    UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(8*OffWidth, 72*OffHeight, 355*OffWidth, 195*OffHeight)];
    [image setBackgroundColor:[UIColor clearColor] ];
    image.layer.cornerRadius=10;
    image.userInteractionEnabled=YES;
    [beiImage addSubview:image];
    
    
    
    UIImageView * headerImage=[[UIImageView alloc]initWithFrame:CGRectMake(13*OffWidth, 13*OffHeight,348*OffWidth, 170*OffHeight)];
    [headerImage setBackgroundColor:[UIColor lightGrayColor]];
    headerImage.userInteractionEnabled = YES;
       [image addSubview:headerImage];
    
    
    _arr=@[@"http://img002.21cnimg.com/photos/album/20140105/m320/5D4D1728D4E00105DA35917A3822E0C7.jpg",@"http://h.hiphotos.baidu.com/image/pic/item/d4628535e5dde7116f03f1c9a4efce1b9d166138.jpg",@"http://img.xiami.net/images/collect/867/67/10369867_1332048318.jpg",@"http://imgsrc.baidu.com/forum/w%3D580/sign=19783db06a63f6241c5d390bb745eb32/64176b63f6246b60e75404fdeaf81a4c500fa284.jpg?v=tbs"];

    
    _sdcScroll = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 348*OffWidth, 170*OffHeight) imageURLStringsGroup:_arr];
    [_sdcScroll setDelegate:self];
    [headerImage addSubview:_sdcScroll];
    
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,375*OffWidth , 667*OffHeight)];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    _tableView.tableHeaderView=image;
    [_tableView setBackgroundColor:[UIColor clearColor]];
    [beiImage setUserInteractionEnabled:YES];
    [beiImage addSubview:_tableView];
    
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.removeFromSuperViewOnHide = YES;
    self.HUD.labelText = @"loading";
    [self.view addSubview:self.HUD];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"jier.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(nicn)];
    
    
}
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
//    if (index==0) {
//        Dc_LunFViewController * lunF=[[Dc_LunFViewController alloc]init];
//        NSLog(@"321231312");
//        [self.navigationController pushViewController:lunF animated:YES];
//    }
    
}
-(void)http{
    [DcAFNetWorkTool getUrl:@"http://api.dongting.com/frontpage/frontpage?f=f0&s=s200&v=v8.1.1.2015081317&version=0" body:nil result:DcJSON headerFlile:nil success:^(id result) {
        NSMutableArray * resultArray=[result objectForKey:@"data"];
        NSMutableDictionary *strDic=resultArray[2];
        NSMutableArray *arr=strDic[@"data"];
        NSMutableDictionary *secDic=arr[0];
        NSMutableArray *secArr=secDic[@"songs"];

        
        
        for (NSMutableDictionary * dic in secArr) {
            DcFirstModel * model=[[DcFirstModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_array addObject:model];
    
        }
        
        for (int i=0; i<_array.count; i++) {
            DcFirstModel * mo=[_array objectAtIndex:i];
            if (mo.auditionList==nil) {
                [_array removeObjectAtIndex:i];
            }
        }
     
        NSMutableDictionary * dicstr=resultArray[3];
        NSMutableArray * hotArr=dicstr[@"data"];
        
        for (NSMutableDictionary * dis in hotArr) {
            DcFirstModel * model=[[DcFirstModel alloc]init];
            [model setValuesForKeysWithDictionary:dis];
            [_douArray addObject:model];

            NSMutableDictionary * d=resultArray[6];
            NSMutableArray * dArray=d[@"data"];
            for (NSMutableDictionary * di in dArray) {
                DcFirstModel * model=[[DcFirstModel alloc]init];
                [model setValuesForKeysWithDictionary:di];
                [_xinArray addObject:model];
            }
        }
        [_tableView reloadData];
        [self.HUD setHidden:YES];
    } failure:^(NSError *error) {
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"大家都在听";
    }else if(section==1){
        return @"热门歌单";
    }else{
        return @"亚洲新歌榜";
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *newView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375*OffWidth, 20*OffHeight)];
    newView.backgroundColor=[UIColor clearColor];
    newView.alpha=1;
    
    UILabel *leftLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120*OffWidth, 20*OffHeight)];
    [newView addSubview:leftLabel];
    [leftLabel release];
    leftLabel.font=[UIFont fontWithName:@"Helvetica" size:18];
    leftLabel.textColor=[UIColor whiteColor];
    
    if (section==0) {
        leftLabel.text=@"大家都在听";
    }
    else if (section==1){
        leftLabel.text=@"热门歌单";
    }else if (section){
        leftLabel.text=@"亚洲新歌榜";
    }
    return newView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return _array.count;
    }
    else if(section==1){
        return _douArray.count-5;
    }else{
        return _xinArray.count-17;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 100 *OffHeight;
    }else if(indexPath.section==1){
        return 320*OffHeight;
        
    }else {
        return 135*OffHeight;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        static NSString * cellId1=@"cell1";
       Dc_FTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId1];
        if (cell==0) {
            cell=[[Dc_FTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault                                   reuseIdentifier:cellId1];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
        DcFirstModel * model=[_array objectAtIndex:indexPath.row];
        [cell model:model];
    return cell;
    }else if(indexPath.section==1){
        static NSString * cellId2=@"cell2";
        Dc_HotTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId2];
        if (cell==0) {
            cell=[[Dc_HotTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault                                   reuseIdentifier:cellId2];
            cell.backgroundColor=[UIColor clearColor];
        }
        cell.delegate=self;
        cell.modelarray=_douArray;
             return cell;
    }
    else{
        static NSString * cellId3=@"cell3";
        Dc_FCTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId3];
        if (cell==0) {
            cell=[[Dc_FCTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault                                   reuseIdentifier:cellId3];
         cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor clearColor];
        }
        DcFirstModel * mod=[_xinArray objectAtIndex:indexPath.row];
        [cell model:mod];
        return cell;
   }
}
//协议传值
-(void)chuanaction:(NSMutableDictionary * )action chuanimage:(NSString *)picUrl name:(NSString *)name author:(NSString *)author{
    Dc_HotViewController * hotView=[[Dc_HotViewController alloc]init];
    hotView.action =action;
    hotView.picUrl=picUrl;
    hotView.name=name;
    hotView.author=author;
    [self.navigationController pushViewController:hotView animated:YES]; 
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
  
   
//        if (indexPath.row==0) {
//            
//            self.block(YES);
//            NSLog(@"222");
//        }
        
        DcFirstModel *lis = [_array objectAtIndex:indexPath.row];
        NSString *str=lis.picUrl;
       
        NSURL *url=[NSURL URLWithString:str];
        [Dc_Dan shareDan].image=url;
        NSDictionary *dic = [lis.auditionList objectAtIndex:0];
        NSString *url1 = [dic objectForKey:@"url"];
        [Dc_Dan shareDan].url = url1;
        [Dc_Dan shareDan].singname = lis.name;
        [Dc_Dan shareDan].singername=lis.singerName;
        [Dc_Dan shareDan].index = indexPath.row;
        [Dc_Dan shareDan].urlarray = _array;
        

        if (lis.auditionList.count >2) {
            NSDictionary *dic = [lis.auditionList objectAtIndex:1];
            NSString *url = [dic objectForKey:@"url"];
            [self playerMusicUrl:url];
        }if (lis.auditionList.count==1) {
            NSDictionary *dic = [lis.auditionList objectAtIndex:0];
            NSString *url = [dic objectForKey:@"url"];
            [self playerMusicUrl:url];
            
        
        
            
        }
    }if (indexPath.section==2) {
        Dc_XinGeViewController * xinge=[[Dc_XinGeViewController alloc]init];
        [self.navigationController pushViewController:xinge animated:YES];
     }
}
- (void)playerMusicUrl:(NSString *)url
{
    
    Dc_PlayerSTK *stk = [Dc_PlayerSTK shareHandleController];
    [stk play:url];
    
}
-(void)nicn{
    Dc_PlayerViewController *play=[[Dc_PlayerViewController alloc]init];
    [self presentViewController:play  animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
